#pragma once
#include <iostream>
#include<string>
#include<functional>
#include"socket.hpp"
#include"InetAddr.hpp"
#include"Protocol.hpp"
#include"NetCal.hpp"
#include"connection.hpp"
using namespace socket_ns;
using process_t=std::function<std::shared_ptr<Response>(std::shared_ptr<Request>)> ;
class PackagePrasse
{
public:
    PackagePrasse(NetCal cal)
    :_cal(cal)
    {
    }
    ~PackagePrasse()
    {
    }
    // void IOExcute(SockSPtr socket,InetAddr& client)
    void Excute(Connection* conn)
    {
        while(true)
        {
            std::string package=Decode(conn->Inbuffer());
            //说明读取的数据不完整
            if(package.empty())
            break;
            //到这package中一定是一条完整序列化的信息
            std::cout<<"package: "<<package<<std::endl;
            auto req=Factory::BuildRequestDefault();
            req->DeSerialize(package);
            //业务处理
            auto resp=_cal.calculator(req);
            //序列化数据应答
            std::string respjson;
            resp->Serialize(&respjson); //序列化
            respjson=Encode(respjson);//添加报头

            conn->AppendOutbuffer(respjson);//发回连接的输出缓冲区
        }
        //这里一定有一条完整的报文

        //方法一：直接调用发送
        if(!conn->Outbuffer().empty())
        {
            conn->_hander_sender(conn);
        }
    }
private:
    NetCal _cal;
};